import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import LayoutView from '../views/LayoutView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/',
      component: LayoutView,
      children: [
        {
          path: '',
          name: 'home',
          component: () => import('../views/HomeView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/dishes',
          name: 'dishes',
          component: () => import('../views/DishManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/orders',
          name: 'orders',
          component: () => import('../views/OrderManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/users',
          name: 'users',
          component: () => import('../views/UserManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/roles',
          name: 'roles',
          component: () => import('../views/RoleManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/role/:roleId/permissions',
          name: 'rolePermissions',
          component: () => import('../views/RolePermission.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/permissions',
          name: 'permissions',
          component: () => import('../views/PermissionManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/tables',
          name: 'tables',
          component: () => import('../views/TableManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/categories',
          name: 'categories',
          component: () => import('../views/CategoryManagement.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/sales',
          name: 'sales',
          component: () => import('../views/SalesStatistics.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/reports',
          name: 'reports',
          component: () => import('../views/ReportCenter.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/files',
          name: 'files',
          component: () => import('../views/FileUpload.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/profile',
          name: 'profile',
          component: () => import('../views/ProfileView.vue'),
          meta: { requiresAuth: true }
        },
        {
          path: '/settings',
          name: 'settings',
          component: () => import('../views/SettingsView.vue'),
          meta: { requiresAuth: true }
        },
        // 通配符路由，处理所有未匹配的路径
        {
          path: '/:pathMatch(.*)*',
          redirect: '/permissions'
        }
      ]
    }
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin-token')
  
  if (to.meta.requiresAuth && !token) {
    // 需要认证但没有token，跳转到登录页
    next('/login')
  } else if (to.path === '/login' && token) {
    // 已经登录了还访问登录页，重定向到首页
    next('/')
  } else {
    // 其他情况放行
    next()
  }
})

export default router